package logic

import (
	"context"
	"fmt"
	"rent/models"
	"strings"

	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *rentrpc.CommentAddRequest) (*rentrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	if strings.Contains(in.Content, "欺诈") || strings.Contains(in.Content, "伪劣") {
		return nil, fmt.Errorf("包含敏感词")
	}
	var comment models.Comment
	comment = models.Comment{
		Content:     in.Content,
		UserId:      int(in.UserId),
		CommodityId: int(in.CommodityId),
	}
	models.DB.Create(&comment)
	return &rentrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
